package com.canaan.business.service.agv.impl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import cn.hutool.http.HttpUtil;
import com.canaan.business.domain.agv.AgvRcsRecord;
import com.canaan.business.mapper.agv.AgvRcsRecordMapper;
import com.canaan.business.service.agv.IAgvRcsRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.canaan.common.core.domain.R;
import com.canaan.common.utils.DateUtils;

import cn.hutool.json.JSONObject;
import io.netty.util.internal.StringUtil;

/**
 * rcs调用记录Service业务层处理
 *
 * @author
 * @date 2023-03-02
 */
@Service
public class AgvRcsRecordServiceImpl implements IAgvRcsRecordService {
    @Resource
    private AgvRcsRecordMapper agvRcsRecordMapper;

    /**
     * 查询rcs调用记录
     *
     * @param id rcs调用记录主键
     * @return rcs调用记录
     */
    @Override
    public AgvRcsRecord selectAgvRcsRecordById(Long id) {
        return agvRcsRecordMapper.selectAgvRcsRecordById(id);
    }

    /**
     * 查询rcs调用记录列表
     *
     * @param agvRcsRecord rcs调用记录
     * @return rcs调用记录
     */
    @Override
    public List<AgvRcsRecord> selectAgvRcsRecordList(AgvRcsRecord agvRcsRecord) {
        return agvRcsRecordMapper.selectAgvRcsRecordList(agvRcsRecord);
    }

    /**
     * 新增rcs调用记录
     *
     * @param agvRcsRecord rcs调用记录
     * @return 结果
     */
    @Override
    public int insertAgvRcsRecord(AgvRcsRecord agvRcsRecord) {
        agvRcsRecord.setCreateTime(DateUtils.getNowDate());
        return agvRcsRecordMapper.insertAgvRcsRecord(agvRcsRecord);
    }

    /**
     * 批量新增rcs调用记录
     *
     * @param agvRcsRecordList rcs调用记录列表
     * @return 结果
     */
    @Override
    public int batchInsertAgvRcsRecord(List<AgvRcsRecord> agvRcsRecordList) {
        Date currerTime = DateUtils.getNowDate();
        for (AgvRcsRecord agvRcsRecord : agvRcsRecordList){
            agvRcsRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<agvRcsRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > agvRcsRecordList.size()) {
                endIndex = agvRcsRecordList.size();
            }
            insertNum = insertNum + agvRcsRecordMapper.batchInsertAgvRcsRecord(agvRcsRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改rcs调用记录
     *
     * @param agvRcsRecord rcs调用记录
     * @return 结果
     */
    @Override
    public int updateAgvRcsRecord(AgvRcsRecord agvRcsRecord) {
        agvRcsRecord.setUpdateTime(DateUtils.getNowDate());
        return agvRcsRecordMapper.updateAgvRcsRecord(agvRcsRecord);
    }

    /**
     * 批量修改rcs调用记录
     *
     * @param agvRcsRecordList rcs调用记录列表
     * @return 结果
     */
    @Override
    public int batchUpdateAgvRcsRecord(List<AgvRcsRecord> agvRcsRecordList) {
        Date currerTime = DateUtils.getNowDate();
        for (AgvRcsRecord agvRcsRecord : agvRcsRecordList){
            agvRcsRecord.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<agvRcsRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > agvRcsRecordList.size()) {
                endIndex = agvRcsRecordList.size();
            }
            updateNum = updateNum + agvRcsRecordMapper.batchUpdateAgvRcsRecord(agvRcsRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除rcs调用记录
     *
     * @param ids 需要删除的rcs调用记录主键集合
     * @return 结果
     */
    @Override
    public int deleteAgvRcsRecordByIds(Long[] ids) {
        return agvRcsRecordMapper.deleteAgvRcsRecordByIds(ids);
    }

    /**
     * 删除rcs调用记录信息
     *
     * @param id rcs调用记录主键
     * @return 结果
     */
    @Override
    public int deleteAgvRcsRecordById(Long id) {
        return agvRcsRecordMapper.deleteAgvRcsRecordById(id);
    }

    /**
     * 调用 rcs接口 失败记录
     *
     * @param rcsRecord  rcs记录实体
     * @param fail 失败实体
     * @param e 捕捉异常
     */
    @Override
    public void setRcsFailRecord(AgvRcsRecord rcsRecord, R fail, Exception e) {
        rcsRecord.setResponse(new JSONObject(fail).toString());
        rcsRecord.setStatus("EXCEPTION");
        rcsRecord.setErrorMsg(e.getMessage());
        this.insertAgvRcsRecord(rcsRecord);
    }


    /**
     * 重发
     *
     * @param id id
     * @return com.canaan.wcs.entity.agv.AgvRcsRecord
     */
    @Override
    public AgvRcsRecord resetById(Long id) {
        AgvRcsRecord agvRcsRecord = this.selectAgvRcsRecordById(id);
        if (!StringUtil.isNullOrEmpty(agvRcsRecord.getAgvUrl())){
            try {
                String httpStart = "/";
                if (!agvRcsRecord.getMethod().contains(httpStart)){
                    agvRcsRecord.setMethod(httpStart+agvRcsRecord.getMethod());
                }
                HttpUtil.post(agvRcsRecord.getAgvUrl() + agvRcsRecord.getMethod(), new JSONObject(agvRcsRecord.getParam()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        agvRcsRecord.setStatus("SUCCESS");
        this.updateAgvRcsRecord(agvRcsRecord);
        return agvRcsRecord;
    }
}
